package com.eascs.web.o2o.scheduler.history;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.scheduler.entity.PaymentsPlan;

@Service
public class PaymentPlanService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	BaseService baseService;
	

	public void savePaymentPlan() {
		//每一笔还款计划的唯一标示是refund_no，而不是bill_code，因为同一个单可能同时有两笔还款计划（银行的，怡亚通的）
		String sql="select r.loadapplycode as bill_code ,l.refundno as refund_no, l.termno as periods  ,r.loanername as customer_name, l.interestdate as plan_date,l.repaymentdate as repayment_date,l.principalremained as residual_principal,l.isfinish as is_finish,l.isexpire as is_expire,l.arprincipal as pay_principal,l.arinterest as pay_interest,l.hrprincipal as fact_principal,l.hrinterest as fact_interest ,r.creatorname as oprate_psn ,r.createdate as oprate_time,'0'  as source_type  from eadb.o2o_refund r inner join eadb.o2o_refundline l on r.refundno = l.refundno WHERE R.islock='N' ";
		//1.同步老系统的还款计划
		logger.info("同步老系统的还款计划开始......");
		try {
			baseService.loadData(sql, PaymentsPlan.class);
		} catch (Exception e) {
			logger.error("同步老系统的还款计划出现异常......");
			throw e;
		}
		logger.info("同步老系统的还款计划结束......");
		
		
		// 2.同步新系统的还款计划
		logger.info("同步新系统的还款计划开始......");
		try {
			sql = "select t.bill_code ,t.name customer_name,t.id_number, t.periods,t.payments_date  plan_date ,t.residual_principal,t.pay_principal,t.pay_interest,t.pay_total,t.oprate_psn,t.oprate_time,t.repayment_status ,'1' source_type from o2o_payments_plan t where t.is_del='N' ";
			baseService.loadServiceData(sql, PaymentsPlan.class, " delete from payments_plan where source_type='1' ");
		} catch (Exception e) {
			logger.error("同步新系统的还款计划出现异常......");
			throw e;
		}
		logger.info("同步新系统的还款计划结束......");
		
	}
	
	
	
}
